import enum
import math 

class MACharacter:
    def __init__(self):
        self.skill_shimen = [0] * 7
        self.skill_xiulian = [0] * 5
        self.skill_pet_xiulian = [0] * 5
        self.skill_bangpai = [0] * 18

# shimen

limit_skill_shimen = 181
skill_shimendata = []
def init_shimen():
    # 计算上一级到下一级的所需数据
    for i in range(limit_skill_shimen):
        if 0 <= i <= 30:
            num = int((math.pow(i, 4) / 30 + math.pow(i, 3)) / 5 + i * 20)
            exp = int(num * 4 / 5)
            mhb = int(num * 3 / 10)
            skill_shimendata.append({"id": i, "num": num, "exp": exp, "mhb": mhb})
        elif 31 <= i <= 150:
            num = int((math.pow(i, 4) / 30 + math.pow(i, 3)) / 5 + i * 20)
            exp = int(num * 4 / 5)
            mhb = int(num * 3 / 10)
            skill_shimendata.append({"id": i, "num": num, "exp": exp, "mhb": mhb})
        elif 151 <= i <= 155:
            num = int(((math.pow(i, 4) / 30 + math.pow(i, 3)) / 5 + i * 20) * 2)
            exp = int(num * 4 / 5)
            mhb = int(num * 3 / 10)
            skill_shimendata.append({"id": i, "num": num, "exp": exp, "mhb": mhb})
        elif 156 <= i <= 160:
            num = int(((math.pow(i, 4) / 30 + math.pow(i, 3)) / 5 + i * 20) * 3)
            exp = int(num * 4 / 5)
            mhb = int(num * 3 / 10)
            skill_shimendata.append({"id": i, "num": num, "exp": exp, "mhb": mhb})
        elif 161 <= i <= 170:
            num = int(int((math.pow(i, 3) * int(i / 30 + 1) / 5) + i * 20) * 3)
            exp = int((num / 4 * 5) * 4 / 5)
            mhb = int(num / 10 * 3)
            skill_shimendata.append({"id": i, "num": num, "exp": exp, "mhb": mhb})
        elif 171 <= i <= 175:
            num = int(int((math.pow(i, 3) * int(i / 30 + 1) / 5) + i * 20) * 4)
            exp = int((num / 4 * 5) * 4 / 5)
            mhb = int(num / 10 * 3)
            skill_shimendata.append({"id": i, "num": num, "exp": exp, "mhb": mhb})
        elif 176 <= i <= 180:
            num = int(int((math.pow(i, 3) * int(i / 30 + 1) / 5) + i * 20) * 5)
            exp = int((num / 4 * 5) * 4 / 5)
            mhb = int(num / 10 * 3)
            skill_shimendata.append({"id": i, "num": num, "exp": exp, "mhb": mhb})
        
def add_sum_shimen(form, to):
    exp = 0
    mhb = 0
    for i in range(form + 1, to + 1):
        exp += int(skill_shimendata[i]['exp'])
        mhb += int(skill_shimendata[i]['mhb'])
    return {'exp': exp, 'mhb': mhb}

def cal_skill_shimen(form, to):
    if form < 0 or to >= limit_skill_shimen:
        return None, False
    
    return add_sum_shimen(form, to), True
        
skill_shimen_cost_map = {
    1: 6,
    2: 18,
    3: 37,
    4: 65,
    5: 103,
    6: 154,
    7: 221,
    8: 307,
    9: 417,
    10: 556,
    11: 730,
    12: 946,
    13: 1212,
    14: 1537,
    15: 1930,
    16: 2402,
    17: 2965,
    18: 3632,
    19: 4418,
    20: 5337,
    21: 6407,
    22: 7645,
    23: 9071,
    24: 10707,
    25: 12575,
    26: 14699,
    27: 17103,
    28: 19817,
    29: 22867,
    30: 26287,
    31: 30107,
    32: 34362,
    33: 39087,
    34: 44321,
    35: 50104,
    36: 56478,
    37: 63487,
    38: 71177,
    39: 79596,
    40: 88795,
    41: 98827,
    42: 109747,
    43: 121612,
    44: 134483,
    45: 148421,
    46: 163491,
    47: 179761,
    48: 197301,
    49: 216183,
    50: 236482,
    51: 258277,
    52: 281648,
    53: 306679,
    54: 333456,
    55: 362069,
    56: 392610,
    57: 425175,
    58: 459862,
    59: 496773,
    60: 536013,
    61: 577689,
    62: 621913,
    63: 668799,
    64: 718465,
    65: 771033,
    66: 826628,
    67: 885377,
    68: 947413,
    69: 1012871,
    70: 1081890,
    71: 1154613,
    72: 1231187,
    73: 1311762,
    74: 1396492,
    75: 1485535,
    76: 1579053,
    77: 1677213,
    78: 1780184,
    79: 1888140,
    80: 2001259,
    81: 2119724,
    82: 2243722,
    83: 2373443,
    84: 2509083,
    85: 2650841,
    86: 2798921,
    87: 2953532,
    88: 3114887,
    89: 3283203,
    90: 3458703,
    91: 3641613,
    92: 3832164,
    93: 4030593,
    94: 4237141,
    95: 4452054,
    96: 4675583,
    97: 4907983,
    98: 5149516,
    99: 5400447,
    100: 5661046,
    101: 5931590,
    102: 6212360,
    103: 6503643,
    104: 6805730,
    105: 7118918,
    106: 7443510,
    107: 7779813,
    108: 8128141,
    109: 8488813,
    110: 8862152,
    111: 9248489,
    112: 9648160,
    113: 10061506,
    114: 10488874,
    115: 10930617,
    116: 11387094,
    117: 11858670,
    118: 12345715,
    119: 12848606,
    120: 13367726,
    121: 13903463,
    122: 14456212,
    123: 15026375,
    124: 15614359,
    125: 16220577,
    126: 16845450,
    127: 17489404,
    128: 18152872,
    129: 18836293,
    130: 19540112,
    131: 20264783,
    132: 21010764,
    133: 21778521,
    134: 22568526,
    135: 23381259,
    136: 24217206,
    137: 25076859,
    138: 25960719,
    139: 26869292,
    140: 27803091,
    141: 28762638,
    142: 29748460,
    143: 30761093,
    144: 31801079,
    145: 32868967,
    146: 33965314,
    147: 35090685,
    148: 36245650,
    149: 37430789,
    150: 38646689,
    151: 41141197,
    152: 43699616,
    153: 46323165,
    154: 49013079,
    155: 51770606,
    156: 56010213,
    157: 60355058,
    158: 64807085,
    159: 69368262,
    160: 74040581,
    161: 74490622,
    162: 79085185,
    163: 83765323,
    164: 88532092,
    165: 93386557,
    166: 98329783,
    167: 103362847,
    168: 108486832,
    169: 113702827,
    170: 119011927,
    171: 126216334,
    172: 133547824,
    173: 141007888,
    174: 148598017,
    175: 156319717,
    176: 166138192,
    177: 176124919,
    178: 186281812,
    179: 196610791,
    180: 208863391,
}

#xiulian
skill_xiulian_cost_map = {
    1: 450000,
    2: 1080000,
    3: 1950000,
    4: 3120000,
    5: 4650000,
    6: 6600000,
    7: 9030000,
    8: 12000000,
    9: 15570000,
    10: 19800000,
    11: 24750000,
    12: 30480000,
    13: 37050000,
    14: 44520000,
    15: 52950000,
    16: 62400000,
    17: 72930000,
    18: 84600000,
    19: 97470000,
    20: 111600000,
    21: 127050000,
    22: 143880000,
    23: 162150000,
    24: 181920000,
    25: 203250000
}

# pet xiulian
xiulianguo_exp = 150
skill_pet_xiulian_exp_map ={
    1: 150,
    2: 360,
    3: 650,
    4: 1040,
    5: 1550,
    6: 2200,
    7: 3010,
    8: 4000,
    9: 5190,
    10: 6600,
    11: 8250,
    12: 10160,
    13: 12350,
    14: 14840,
    15: 17650,
    16: 20800,
    17: 24310,
    18: 28200,
    19: 32490,
    20: 37200,
    21: 42350,
    22: 47960,
    23: 54050,
    24: 60640,
    25: 67750,
}

# bangpai
class SKILL_BANGPAI(enum.Enum):
    qs = 0 #强身
    mx = enum.auto() #冥想
    aq = enum.auto() #暗器技巧
    zy = enum.auto() #中药医理
    zb = enum.auto() #追捕技巧
    tl = enum.auto() #逃离技巧
    ys = enum.auto() #养生之道
    js = enum.auto() #健身术
    qj = enum.auto() #巧匠之术
    rl = enum.auto() #熔炼技巧
    ls = enum.auto() #灵石技巧
    qz = enum.auto() #强壮
    ss = enum.auto() #神速
    dz = enum.auto() #打造技巧
    cf = enum.auto() #裁缝技巧
    lj = enum.auto() #炼金术
    pr = enum.auto() #烹饪技巧
    cl = enum.auto() #淬灵之术
    
    
expdata1 = []  # 强身术
expdata2 = []  # 冥想/暗器技巧/中药医理/追捕技巧/逃离技巧/养生之道/健身术/巧匠之术/熔炼技巧
expdata3 = []  # 灵石技巧
expdata4 = []  # 强壮/神速
expdata5 = []  # 打造技巧/裁缝技巧/炼金术/烹饪技巧/淬灵之术

skill_bangpai_map ={
    SKILL_BANGPAI.qs: 1,
    SKILL_BANGPAI.mx: 2,
    SKILL_BANGPAI.aq: 2,
    SKILL_BANGPAI.zy: 2,
    SKILL_BANGPAI.zb: 2,
    SKILL_BANGPAI.tl: 2,
    SKILL_BANGPAI.ys: 2,
    SKILL_BANGPAI.js: 2,
    SKILL_BANGPAI.qj: 2,
    SKILL_BANGPAI.rl: 2,
    SKILL_BANGPAI.ls: 3,
    SKILL_BANGPAI.qz: 4,
    SKILL_BANGPAI.ss: 4,
    SKILL_BANGPAI.dz: 5,
    SKILL_BANGPAI.cf: 5,
    SKILL_BANGPAI.lj: 5,
    SKILL_BANGPAI.pr: 5,
    SKILL_BANGPAI.cl: 5,
}

def formula(id, lv):
    if id == '1':
        return int((int(math.pow(lv, 4) / 30) + math.pow(lv, 3)) / 5) + lv * 20

    elif id == '2':
        return int((int(math.pow(lv + 15, 4) / 30) + math.pow(lv + 15, 3)) / 5) + (lv + 15) * 20

    elif id == '3':
        return int((int(math.pow(lv, 4) / 30) + math.pow(lv, 3)) / 5) + lv * 20


limit_bangpai_1 = 141
limit_bangpai_2 = 161
limit_bangpai_3 = 121
limit_bangpai_4 = 41
limit_bangpai_5 = 161

def init_bangpai():
    for a in range(141):
        mhb = int(formula('3', a) * 3 / 10) 
        if 0 <= a <= 120:
            exp = int(formula('1', a) * 4 / 5)
        elif 121 <= a <= 130:
            exp = int(formula('1', a) * 6 / 5)
        elif 131 <= a <= 140:
            exp = int(formula('1', a) * 8 / 5)
        expdata1.append({'id': a, 'exp': exp, 'bg_use': a, 'mhb': mhb})
        
    for b in range(161):
        if 0 <= b <= 150:
            exp = int(formula('1', b) * 4 / 5)
            mhb = int(formula('3', b) * 3 / 10)
        elif 151 <= b <= 155:
            exp = int(formula('1', b) * 8 / 5)
            mhb = int(exp * 3 / 10)
        elif 156 <= b <= 160:
            exp = int(formula('1', b) * 12 / 5)
            mhb = int(exp * 3 / 10)
        expdata2.append({'id': b, 'exp': exp, 'bg_use': b, 'mhb': mhb})

    for c in range(121):
        if 0 <= c <= 80:
            exp = int(formula('2', c) * 4 / 5)
        elif 81 <= c <= 120:
            exp = int(formula('2', c) * 6 / 5)
        mhb = int(exp * 3 / 10)
        expdata3.append({'id': c, 'exp': exp, 'bg_use': c, 'mhb': mhb})

    for d in range(41):
        exp = int((d+5)**2 * 20000 + 1000000)
        mhb = int(exp / 2)
        expdata4.append({'id': d, 'exp': exp, 'bg_use': d, 'mhb': mhb})

    for e in range(161):
        if 0 <= e <= 150:
            exp = int(formula('1', e) * 4 / 5)
        elif 151 <= e <= 160:
            baseExp = formula('1', e)
            exp = int((int(baseExp * (e-100) / 100) + baseExp) * 4 / 5)
        mhb = int(formula('3', e) * 3 / 10)
        expdata5.append({'id': e, 'exp': exp, 'bg_use': e, 'mhb': mhb})

def cal_skill_bangpai(skill_bp, s, e):
    t = skill_bangpai_map[skill_bp]
    if t == 1:
        return add_sum_bangpai(s, e, expdata1), True
    elif t == 2:
        return add_sum_bangpai(s, e, expdata2), True
    elif t == 3:
        return add_sum_bangpai(s, e, expdata3), True
    elif t == 4:
        return add_sum_bangpai(s, e, expdata4), True
    elif t == 5:
        return add_sum_bangpai(s, e, expdata5), True
    else:
        return None, False


def add_sum_bangpai(form, to, resultdata):
    exp = 0
    mhb = 0
    bg_use = 0
    for i in range(form + 1, to + 1):
        exp += int(resultdata[i]['exp'])
        mhb += int(resultdata[i]['mhb'])
        bg_use += int(resultdata[i]['bg_use'])
    return {'exp': exp, 'mhb': mhb/2, 'bg_use': bg_use, 'bg_need': bg_use + to*5 }

qianyuandan_mhb_cost_map = [
    4468171,
    5569343,
    6906847,
    8503933,
    10383652,
    12569051,
    15083183,
    15195518,
    14440238,
]

qianyuandan_exp_cost_map = [
    22341872,
    27846728,
    34534248,
    42519680,
    51918272,
    62845272,
    75415928,
    75977600,
    72201200,
]

def add_sum_qianyuandan(form, to):
    exp = 0
    mhb = 0
    for i in range(form, to):
        exp += qianyuandan_exp_cost_map[i]
        mhb += qianyuandan_mhb_cost_map[i]
    return {'exp': exp, 'mhb': mhb}

def cal_qianyuandan(form, to):
    if form < 0 or to > 9:
        return None, False
    
    return add_sum_qianyuandan(form, to), True
        

if __name__ == "__main__":
    
    init_shimen()
    print(f"师门技能 0-180: {cal_skill_shimen(0, 180)}")
    
    init_bangpai()
    print(f"强身 0-{limit_bangpai_1-1}: {cal_skill_bangpai(SKILL_BANGPAI.qs, 0, limit_bangpai_1-1)}")
    print(f"暗器 0-{limit_bangpai_2-1}: {cal_skill_bangpai(SKILL_BANGPAI.aq, 0, limit_bangpai_2-1)}")
    print(f"灵石技巧 0-{limit_bangpai_3-1}: {cal_skill_bangpai(SKILL_BANGPAI.ls, 0, limit_bangpai_3-1)}")
    print(f"神速 0-{limit_bangpai_4-1}: {cal_skill_bangpai(SKILL_BANGPAI.ss, 0, limit_bangpai_4-1)}")
    print(f"打造 0-{limit_bangpai_5-1}: {cal_skill_bangpai(SKILL_BANGPAI.dz, 0, limit_bangpai_5-1)}")
    
    print(f"乾元丹 0-9: {cal_qianyuandan(0, 9)}")